package com.example.springboot008webmvcconfigurer.settings;

import com.example.springboot008webmvcconfigurer.Formatter.DeviceFormatter;
import com.example.springboot008webmvcconfigurer.Interceptor.AuthInterceptor;
import com.example.springboot008webmvcconfigurer.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description: SpringMVC配置：使用JavaConfig的方式配置SpringMVC,代替原来的XMl文件
 * @author: uu
 * @data: 2023/7/4 上午 12:24
 * @version: v1.0
 */
@Configuration
public class MvcSettings implements WebMvcConfigurer {
    // 页面直达视图页面
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 配置页面控制
        registry.addViewController("/welcome").setViewName("index");

    }
    //登记Formatter方法
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DeviceFormatter());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        AuthInterceptor authInterceptor = new AuthInterceptor();
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(authInterceptor);
        InterceptorRegistration interceptorRegistration1 = registry.addInterceptor(loginInterceptor);
        interceptorRegistration.addPathPatterns("/article/**").excludePathPatterns("/article/query").order(2);
        interceptorRegistration1.addPathPatterns("/**").excludePathPatterns("/article/query").order(1);
    }

}
